function sum(num, price){
    return parseFloat(num) * parseFloat(price);
}

function operate($this, price){
    let quantity    = $this.siblings('.count-num').text(),
        itemId      = $this.parent().siblings('.cell-id').text().trim(),
        arg         = {
            'quantity' : quantity,
            'itemId'   : itemId
        },
        single_price = sum(quantity,price);

    $this.parent().siblings('.cell-total').text('$'+single_price);

    $.ajax({
        type    : 'POST',
        url     : 'updateCart',
        data    : arg,
        success : function (data) {
            $('.total-price').text('$' + data);
        },
        error   : function (errorMsg){
            console.log(errorMsg);
        }
    });
}

$(function (){
    if($('.total-price').text() !== '$0.00'){
        $('#information').hide();
    }
    // 判断后发送请求
    let innerHTMLStr1,innerHTMLStr2;
    $('.add').click(function (){
        let num     = $(this).siblings('.count-num').text(),
            price   = $(this).parent().siblings('.cell-price').text().trim().substring(1),
            $this   = $(this);
        if(num === '5'){
            $(this).siblings('.judge_Msg').text('Five items at most!');
        } else {
            // 发送ajax请求，修改cart中的quantity,以下代码可以重构封装
            $(this).siblings('.judge_Msg').text('');
            $(this).siblings('.count-num').text(parseInt(num) + 1);

            // $(this).siblings('.cell-total').text(sum(parseFloat(num) + 1, ))
            operate($this,price);
        }
    });
    $('.minus').click(function (){
        let num     = $(this).siblings('.count-num').text(),
            price   = $(this).parent().siblings('.cell-price').text().trim().substring(1),
            $this   = $(this);
        if (num === '1'){
            $(this).siblings('.judge_Msg').text('One item at least!');
        } else {
            // 发送ajax请求，修改cart中的quantity
            $(this).siblings('.judge_Msg').text('');
            $(this).siblings('.count-num').text(parseInt(num) - 1);

            operate($this,price);
        }
    });
    $('.cell-operator .link').click(function (){
        // console.log($(this).text());
        let itemId  = $(this).parent().siblings('.cell-id').text().trim(),
            $this   = $(this),
            arg     = {
                'record'        : 'remove item from cart:',
                'workingItemId' : itemId
            };
        $.ajax({
            type    : 'POST',
            url     : 'removeCartItem',
            data    : arg,
            success : function (data){
                console.log(data);
                $this.parent().parent().remove();
                $('.total-price').text('$' + data);
                if(data === '0'){
                    $('.submit-content').hide();
                    $('#information').show();
                } else {
                    $('.submit-content').show();
                    $('#information').hide();
                }
            },
            error   : function (errorMsg){
                console.log(errorMsg);
            }

        })
    });
});